package com.graduate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.graduate.common.CustomException;
import com.graduate.dto.SetMealDto;
import com.graduate.entity.SetMeal;
import com.graduate.entity.SetmealGoods;
import com.graduate.mapper.SetMealMapper;
import com.graduate.service.SetMealService;
import com.graduate.service.SetmealGoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetMealServiceImpl extends ServiceImpl<SetMealMapper, SetMeal> implements SetMealService {
    @Autowired
    private SetmealGoodsService setmealGoodsService;

    //新增套餐，同时需要保存套餐和商品的关联关系
    @Transactional
    public void saveWithGoods(SetMealDto setMealDto) {
        //保存套餐的基本信息，操作setmeal，执行insert
        this.save(setMealDto);
        List<SetmealGoods> setmealGoods = setMealDto.getSetmealGoods();
        setmealGoods.stream().map((item) ->{
            item.setSetmealId(setMealDto.getId());
            return item;
        }).collect(Collectors.toList());
        //保存套餐和商品的关联信息，操作setmeal_goods，执行insert
        setmealGoodsService.saveBatch(setmealGoods);
    }

    //删除套餐，批量删除套餐
    @Transactional
    public void removeWithGoods(List<Long> ids) {
        //查询套餐状态，确定可否删除
        LambdaQueryWrapper<SetMeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(SetMeal::getId,ids);
        queryWrapper.eq(SetMeal::getStatus,1);
        int count = this.count(queryWrapper);
        if (count > 0){
            //如果不能删除，抛出一个业务异常
            throw  new CustomException("套餐正在售卖中，不能删除");
        }

        //如果可以删除，先删除套餐中的数据--set_mel
        this.removeByIds(ids);

        //删除关系表中的数据
        LambdaQueryWrapper<SetmealGoods> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealGoods::getSetmealId,ids);
        setmealGoodsService.remove(lambdaQueryWrapper);
    }

    @Override
    public SetMealDto getByIdWithGoods(Long id) {
        //查询套餐基本信息，从stemeal表查询
        SetMeal setmeal = super.getById(id);

        SetMealDto setmealDto = new SetMealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);

        //查询当前套餐对应的商品信息，从stemeal_goods表查询
        LambdaQueryWrapper<SetmealGoods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealGoods::getSetmealId,id);
        List<SetmealGoods> setmealGoods = setmealGoodsService.list(queryWrapper);
        setmealDto.setSetmealGoods(setmealGoods);
        return setmealDto;
    }

    @Override
    @Transactional
    public void updateWithGoods(SetMealDto setmealDto) {
        //更新setmeal表基本信息
        super.updateById(setmealDto);

        //清理当前套餐对应商品数据---setmeal_goods表的delete操作
        LambdaQueryWrapper<SetmealGoods> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealGoods::getSetmealId,setmealDto.getId());

        setmealGoodsService.remove(queryWrapper);

        //添加当前提交过来的商品数据---setmeal_goods表的insert操作
        List<SetmealGoods> setmealGoods = setmealDto.getSetmealGoods();

        setmealGoods = setmealGoods.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setmealGoodsService.saveBatch(setmealGoods);

    }
}
